package bounjour.ui;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bounjour.Comment;
import bounjour.Field;
import bounjour.FieldType;
import bounjour.Post;
import bounjour.Tag;
import bounjour.User;

/**
 * Servlet implementation class ViewPost
 */
public class ViewPost extends BaseServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ViewPost() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			int id = Integer.parseInt(request.getParameter("id"));
			Post post = Post.getPostByID(id);
			if (post != null) {
				Boolean editMode = "edit".equals(request.getParameter("mode"));
				map.put("editMode", editMode);
				map.put("message", editMode? "Editing fields of post #" + post.getID() : "Viewing post #" + post.getID());
				map.put("post", post);
				map.put("comments", post.getComments());
				map.put("fields", post.getFields());
				map.put("fieldTypes", FieldType.values());
				process(request, response, "viewPost.ftl");
			} else {
				response.getWriter().write("Post not found");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		User user = getUser(request);
		int postID = Integer.parseInt(request.getParameter("id"));
		Post post = Post.getPostByID(postID);
		if (user != null) {
			if (post != null) {
				if ("addData".equals(request.getParameter("action"))) {				
					Comment comment = new Comment(post.getID());
					comment.setUserID(user.getId());
					for (Field f : post.getFields()) {
						comment.addData(f.getID(), request.getParameter("f_"+f.getID()));
					}
					comment.save();
					response.sendRedirect("ViewPost?id=" + postID);
				}
				else if (request.getParameter("fieldType") != null && request.getParameter("fieldType").length() > 0) {
					Field field = new Field(postID);
					field.setLabel(request.getParameter("fieldName"));
					if (request.getParameter("values") != null) {
						field.setValues(request.getParameter("values").split(","));
					}
					field.setType(FieldType.values()[Integer.parseInt(request.getParameter("fieldType"))]);
					field.save();
					response.sendRedirect("ViewPost?id=" + postID + "&mode=edit");
				}
				else if (request.getParameter("tag") != null && request.getParameter("tag") != "")
				{
					Tag.addTag(user.getId(), post.getID(), request.getParameter("tag"));
					response.sendRedirect("ViewPost?id=" + postID);
				}
			}
		}
		else
		{
			response.sendRedirect("Login");
		}
	}

}
